#include "wlterm.h"
#include <unistd.h>

gboolean expose_handler(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
	PangoColor colors[16] = {
		{0, 0, 0}, /* black */
		{65535/2., 0, 0}, /* dark red */
		{0, 65535/2., 0}, /* dark green */
		{65535/2., 65535/2., 0}, /* dark brown */
		{0, 0, 65535/2.}, /* dark blue */
		{65535/2., 0, 65535/2.}, /* dark magenta */
		{0, 65535/2.,65535/2.}, /* dark cyan */
		{65535*3/4., 65535*3/4., 65535*3/4.}, /* light grey */
		{65535/2., 65535/2., 65535/2.}, /* grey */
		{65535, 0, 0}, /* red */
		{0, 65535, 0}, /* grean */
		{65535, 65535, 0}, /* brown */
		{0, 0, 65535}, /* blue */
		{65535, 0, 65535}, /* magenta */
		{0, 65535, 65535}, /* cyan */
		{65535, 65535, 65535} /* white */
	};
#if 0
	gchar colors[16][] = {
		"#000000", /* black */
		"#800000", /* dark red */
		"#008000", /* dark green */
		"#808000", /* dark brown */
		"#000080", /* dark blue */
		"#800080", /* dark magenta */
		"#008080", /* dark cyan */
		"#C0C0C0", /* light grey */
		"#CCCCCC", /* grey */
		"#FF0000", /* red */
		"#00FF00", /* grean */
		"#FFFF00", /* brown */
		"#0000FF", /* blue */
		"#FF00FF", /* magenta */
		"#00FFFF", /* cyan */
		"#FFFFFF" /* white */
	};
#endif
	int width, height;
	/* widget->allocation not in the gtk+ api */
	width = widget->allocation.width;
	height = widget->allocation.height;
	cairo_t *cr;
	cr = gdk_cairo_create(widget->window);
	cairo_set_source_rgb (cr, 0, 0, 0);
	cairo_move_to(cr, 0, 0);
	cairo_rectangle(cr, 0, 0, width, height);
	cairo_fill(cr);
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_move_to (cr, 0, 34.0);

	PangoLayout *layout;
	PangoFontDescription *font_description;

	font_description = pango_font_description_new ();
	pango_font_description_set_family (font_description, "Wen Quan Yi Zen Hei Mono");
	pango_font_description_set_absolute_size (font_description, 24 * PANGO_SCALE);

	layout = pango_cairo_create_layout (cr);
	pango_layout_set_font_description (layout, font_description);
	pango_font_description_free(font_description);

	WltermSession * session;
	session = (WltermSession*) data;
	int i, j;
	int wl;
	int fg = 15, bg = 0;
	int step = 30;
	char temp[2];
	int bright = 1;
	for (i = 0; i < session->site->rows; i++) {
		for (j = 0; j < session->site->cols; j++) {
			fg = 15;
			bg = 0;
			bg = session->screen[i][j].bg;
			if (flag_is(session->screen[i][j].flag, FLAG_BRIGHT))
				fg = session->screen[i][j].fg + 8;
			else
				fg = session->screen[i][j].fg;
			cairo_move_to(cr, j*12, i*step);
			wl = 1;
			temp[0] = session->screen[i][j].ch;
			if (!isascii(temp[0])) {
				j++;
				temp[1] = session->screen[i][j].ch;
				wl = 2;
			}

			PangoAttribute *attr_bg = pango_attr_background_new(colors[bg].red,
					colors[bg].green, colors[bg].blue);
			PangoAttribute *attr_fg = pango_attr_foreground_new(colors[fg].red,
					colors[fg].green, colors[fg].blue);
			PangoAttrList *attr_list = pango_attr_list_new();
			pango_attr_list_insert(attr_list, attr_bg);
			pango_attr_list_insert(attr_list, attr_fg);
			pango_layout_set_attributes(layout, attr_list);

			gchar *res;
			res = g_convert(temp, wl, "UTF-8", "GBK", NULL, NULL, NULL);
			pango_layout_set_text (layout, res, -1);
			pango_cairo_show_layout (cr, layout);
			g_free(res);
			pango_attr_list_unref(attr_list);
		}
		cairo_move_to(cr, 0, i*step);
	}

	g_object_unref(layout);
	cairo_destroy(cr);
	return TRUE;
}

int main(int argc, char *argv[])
{
	gtk_init(&argc, &argv);

	GtkBuilder *xml = NULL;
	/* load the interface */
	xml = gtk_builder_new();
	gtk_builder_add_from_file(xml, "wlterm.xml", NULL);

	/* connect the signals */
	gtk_builder_connect_signals(xml, NULL);

	WltermSession *session;
	WltermSite site = {
		.address = "bbs.seu.edu.cn",
		.port = 23,
		.login_type = TELNET,
		.rows = SCREEN_ROWS,
		.cols = SCREEN_COLS,
		.encoding = "GBK"
	};
	session = wlterm_session_create(&site);
	wlterm_session_connect(session);
	//test(session);

	/* test mainarea */
	GtkWidget *mainarea;
	mainarea = (GtkWidget*)gtk_builder_get_object(xml, "mainarea");

	g_signal_connect(mainarea, "expose_event",
			G_CALLBACK(expose_handler), session);
	g_signal_connect(mainarea, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	gtk_main();
	wlterm_session_destroy(session);

	return 0;
}
